package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.ClassSettingsModel;
import pl.edu.pw.MidEmu.UI.Controllers.ApplicationsListController;
import pl.edu.pw.MidEmu.UI.Views.ApplicationsListView;

/**
 * Frame with GUI for listing applications
 * @author Mateusz Starodab
 *
 */
public class ApplicationsListFrame extends BaseFrame<JDialog, ApplicationsListController> implements ApplicationsListView
{
	private JFileChooser _fchChooseApp;
	private JButton btnCancel;
	private JButton btnLoadApp;

	private JButton btnRunApp;
	private JList lstFiles;

	private DefaultListModel lstModel;
	private JMenuItem miAddApp;
	private JMenuItem miRemoveApp;

	private JMenuItem miViewApp;

	private MidasComponent parent;
	private JPopupMenu pmContext;
	private JPanel pnlCenter;
	private JPanel pnlContent;

	private JPanel pnlSouth;

	public ApplicationsListFrame(MidasComponent component)
	{
		ApplicationsListController controller = new ApplicationsListController();
		controller.setView(this);
		setController(controller);
		JDialog frame = new JDialog();
		setComponent(frame);

		parent = component;

		getComponent().setTitle(Consts.MainFrame_Title + " - " + parent.getName() + " - " + Consts.ApplicationList_Title);
		getComponent().setSize(300, 320);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getComponent().setModal(true);

		_fchChooseApp = new JFileChooser();
		_fchChooseApp.setMultiSelectionEnabled(false);
		_fchChooseApp.setFileSelectionMode(JFileChooser.FILES_ONLY);
		_fchChooseApp.setFileFilter(new FileNameExtensionFilter(Consts.ApplicationList_FileChooser_ClassFiler, new String[] { Consts.ApplicationList_FileChooser_ClassExtention }));
		_fchChooseApp.addChoosableFileFilter(new FileNameExtensionFilter(Consts.ApplicationList_FileChooser_JarFilter, new String[] { Consts.ApplicationList_FileChooser_ZIPFiler, Consts.ApplicationList_FileChooser_JarExtention }));

		pnlContent = new JPanel();
		pnlContent.setLayout(new BorderLayout());

		pnlCenter = new JPanel();
		pnlCenter.setLayout(new BorderLayout());
		pnlCenter.setBorder(BorderFactory.createTitledBorder(Consts.ApplicationList_PanelCaption));

		lstModel = new DefaultListModel();
		for (ClassSettingsModel model : getController().getClassSetingsHistory(false))
			lstModel.addElement(model);
		lstFiles = new JList(lstModel);
		lstFiles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstFiles.addMouseListener(getController());

		pnlCenter.add(lstFiles, BorderLayout.CENTER);

		pnlContent.add(pnlCenter, BorderLayout.CENTER);

		pnlSouth = new JPanel();
		pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));

		btnLoadApp = new JButton(Consts.ApplicationList_LoadButton);
		btnLoadApp.setName(Consts.ApplicationList_LoadButton);
		btnLoadApp.addActionListener(getController());
		pnlSouth.add(btnLoadApp);

		btnRunApp = new JButton(Consts.ApplicationList_RunButton);
		btnRunApp.setName(Consts.ApplicationList_RunButton);
		btnRunApp.addActionListener(getController());
		pnlSouth.add(btnRunApp);

		btnCancel = new JButton(Consts.ApplicationList_CancelButton);
		btnCancel.setName(Consts.ApplicationList_CancelButton);
		btnCancel.addActionListener(getController());
		pnlSouth.add(btnCancel);

		pnlContent.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlContent);

		pmContext = new JPopupMenu();
		miRemoveApp = new JMenuItem(Consts.ApplicationList_RemoveAppMenu);
		miRemoveApp.setName(Consts.ApplicationList_RemoveAppMenu);
		miRemoveApp.addActionListener(getController());
		miViewApp = new JMenuItem(Consts.ApplicationList_RunAppMenu);
		miViewApp.setName(Consts.ApplicationList_RunAppMenu);
		miViewApp.addActionListener(getController());
		miAddApp = new JMenuItem(Consts.ApplicationList_AddAppMenu);
		miAddApp.setName(Consts.ApplicationList_AddAppMenu);
		miAddApp.addActionListener(getController());

		pmContext.add(miRemoveApp);
		pmContext.add(miViewApp);
		pmContext.addSeparator();
		pmContext.add(miAddApp);
	}

	@Override
	public void close()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}

	/**
	 * @return
	 * @uml.property  name="parent"
	 */
	@Override
	public MidasComponent getParent()
	{
		return parent;
	}

	@Override
	public ClassSettingsModel getSelectedModel()
	{
		if (lstFiles.getSelectedValue() != null)
			return (ClassSettingsModel) lstFiles.getSelectedValue();
		return null;
	}

	public void rebind()
	{
		lstModel.clear();
		for (ClassSettingsModel model : getController().getClassSetingsHistory(true))
			lstModel.addElement(model);

		getComponent().invalidate();
	}

	@Override
	public void repaint(ClassSettingsModel model)
	{
		lstModel.removeElement(model);
		getComponent().repaint();
	}

	@Override
	public void showFileDialog()
	{
		_fchChooseApp.showOpenDialog(getComponent());
		File selectedFile = _fchChooseApp.getSelectedFile();
		if (selectedFile != null)
		{
			String _filePath = selectedFile.getAbsolutePath();
			String tempClassName = null;
			if (_filePath.lastIndexOf('.') > 0)
				tempClassName = _filePath.substring(_filePath.lastIndexOf(File.separatorChar) + 1, _filePath.lastIndexOf('.'));
			else
				tempClassName = _filePath.substring(_filePath.lastIndexOf(File.separatorChar));

			String tempWorkDirName = null;
			tempWorkDirName = _filePath.substring(0, _filePath.lastIndexOf(File.separatorChar));

			ClassSettingsModel model = new ClassSettingsModel();
			model.set_path(_filePath);
			model.set_className(tempClassName);
			model.set_workDir(tempWorkDirName);

			getController().addModel(model);

			lstModel.insertElementAt(model, 0);
			lstFiles.invalidate();
		}
	}

	@Override
	public void showPopupMenu(Component component, int x, int y, boolean enableMenus)
	{
		if (!enableMenus)
		{
			miRemoveApp.setEnabled(false);
			miViewApp.setEnabled(false);
		}
		else
		{
			miRemoveApp.setEnabled(true);
			miViewApp.setEnabled(true);
		}
		pmContext.show(component, x, y);
	}
}
